home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / qbfform.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  10.2 KB  |  277 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++Builder
  3. //Copyright (c) 1987, 1998 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #include <vcl.h>
  7. #pragma hdrstop
  8.  
  9. #include <memory>     //for std::auto_ptr
  10. #include "QBFForm.h"
  11. //---------------------------------------------------------------------------
  12. #pragma resource "*.dfm"
  13. TQueryForm *QueryForm;
  14. //---------------------------------------------------------------------------
  15. __fastcall TQueryForm::TQueryForm(TComponent* Owner)
  16.   : TForm(Owner)
  17. {
  18. }
  19. //---------------------------------------------------------------------------
  20. void __fastcall TQueryForm::FormCreate(TObject *Sender)
  21. {
  22.   Screen->Cursor = Controls::TCursor(crHourGlass);
  23.  
  24.   // Populate the alias list
  25.   ListBox1->Items->Clear();
  26.   Session->GetAliasNames(ListBox1->Items);
  27.  
  28.   // Make sure there are aliases defined
  29.   Screen->Cursor = Controls::TCursor(crDefault);
  30.   if (ListBox1->Items->Count < 1)
  31.     MessageDlg( "There are no database aliases currently defined.  You " \
  32.         "need at least one alias to use this demonstration.",
  33.          mtError, TMsgDlgButtons() << mbOK, 0 );
  34.  
  35.   // Default the drop-down list to the first value in the list
  36.   ComboBox1->ItemIndex = 0;
  37.  
  38. }
  39. //---------------------------------------------------------------------
  40. void __fastcall TQueryForm::BitBtn2Click(TObject *Sender)
  41. {
  42.   AnsiString
  43.     strAlias,            // Alias name selected by the user
  44.     strTable,            // Table name selected by the user
  45.     strField,            // Field name selected by the user
  46.     strValue,            // Field Value entered by the user
  47.     strWhere,            // WHERE clause for the user's query
  48.     strQuote,            // Holds quotes is the query field is text
  49.     strQuery;                    // String used to construct the query
  50.   std::auto_ptr<TResultForm> frmQuery; // The Results form
  51.   char szTemp[1024];
  52.  
  53.   /*
  54.     The following type is used with the Type drop-down
  55.     list.  The text values corresponding with each item is
  56.     described in comments, along with the relevant SQL operators.
  57.     */
  58.  
  59.   typedef enum {
  60.           soNoCondition,  // not field conditions: no WHERE clause
  61.         soEqual,        // equals: =
  62.         soNotEqual,     // is not equal to: <>
  63.         soLessThan,     // is less than: <
  64.         soLessEqual,    // is less than or equal to: <=
  65.         soMoreThan,     // is greater than: >
  66.         soMoreEqual,    // is greater than or equal to: >=
  67.         soStartsWith,   // starts with: LIKE xx%
  68.         soNoStartsWith, // doesn't start with: NOT LIKE xx%
  69.         soEndsWith,     // ends with: LIKE %xx
  70.         soNoEndsWith,   // doesn't end with: NOT LIKE %xx
  71.         soContains,     // contains: LIKE %xx%
  72.         soNoContains,   // doesn't contain: NOT LIKE %xx%
  73.         soBlank,        // is blank:
  74.         soNotBlank,     // is not blank:
  75.         soInside,       // contains only: IN ( xx, yy, zz )
  76.         soOutside       // doesn't contain: NOT IN (xx, yy, zz)
  77.         } etSQLOps;
  78.  
  79.     // Initialize the variables needed to run the query
  80.   if (ListBox1->ItemIndex == -1)
  81.     throw Exception("Can't Run Query: No Alias Selected");
  82.   else
  83.     strAlias = ListBox1->Items->Strings[ListBox1->ItemIndex];
  84.  
  85.   if (ListBox2->ItemIndex == -1)
  86.     throw Exception("Can't Run Query: No Table Selected");
  87.   else
  88.     strTable = ListBox2->Items->Strings[ListBox2->ItemIndex];
  89.  
  90.   if (ListBox3->ItemIndex == -1)
  91.   {
  92.     if (ComboBox1->ItemIndex > soNoCondition)
  93.       throw Exception("Can't Run Query: No Field Selected");
  94.     else
  95.       strField = "";
  96.   }
  97.   else
  98.     strField = ListBox3->Items->Strings[ListBox3->ItemIndex];
  99.  
  100.   if ((Edit1->Text.Length() == 0) && (ComboBox1->ItemIndex > soNoCondition) &&
  101.     (ComboBox1->ItemIndex < soBlank))
  102.     throw Exception("Can't Run Query: No Search Value Entered");
  103.   else
  104.     strValue = Edit1->Text;
  105.  
  106.   /*
  107.     See if the field being search is a string field.  If so, then pad the
  108.     quote string with quotation marks; otherwise, set it to a null value.
  109.     */
  110.   if (strField.Length() != 0)
  111.   {
  112.     if ((Table1->FieldByName(strField)->DataType == ftString) ||
  113.     (Table1->FieldByName(strField)->DataType == ftMemo))
  114.     strQuote = "\"";
  115.      else
  116.     strQuote = " ";
  117.   }
  118.  
  119.   /*
  120.     Construct the WHERE clause of the query based on the user's choice
  121.     in Type.
  122.   */
  123.  
  124.   szTemp[0] = '\0';
  125.   switch (etSQLOps(ComboBox1->ItemIndex))
  126.   {
  127.     case soNoCondition:
  128.     szTemp[0] = '\0';
  129.     break;
  130.     case soEqual:
  131.     wsprintf(szTemp, "%s = %s%s%s", strField.c_str(), strQuote.c_str(), strValue.c_str(), strQuote.c_str());
  132.     break;
  133.     case soNotEqual:
  134.     wsprintf(szTemp, "%s <> %s%s%s", strField.c_str(), strQuote.c_str(), strValue.c_str(), strQuote.c_str());
  135.     break;
  136.     case soLessThan:
  137.     wsprintf(szTemp, "%s < %s%s%s", strField.c_str(), strQuote.c_str(), strValue.c_str(), strQuote.c_str());
  138.     break;
  139.      case soLessEqual:
  140.     wsprintf(szTemp, "%s <= %s%s%s", strField.c_str(), strQuote.c_str(), strValue.c_str(), strQuote.c_str());
  141.     break;
  142.      case soMoreThan:
  143.     wsprintf(szTemp, "%s > %s%s%s", strField.c_str(), strQuote.c_str(), strValue.c_str(), strQuote.c_str());
  144.     break;
  145.      case soMoreEqual:
  146.     wsprintf(szTemp, "%s >= %s%s%s", strField.c_str(), strQuote.c_str(), strValue.c_str(), strQuote.c_str());
  147.     break;
  148.      case soStartsWith:
  149.     wsprintf(szTemp, "%s LIKE %s%s%%%s", strField.c_str(), strQuote.c_str(), strValue.c_str(), strQuote.c_str());
  150.     break;
  151.      case soNoStartsWith:
  152.     wsprintf(szTemp, "%s NOT LIKE %s%s%%%s", strField.c_str(), strQuote.c_str(), strValue.c_str(), strQuote.c_str());
  153.     break;
  154.      case soEndsWith:
  155.     wsprintf(szTemp, "%s LIKE %s%%%s%s", strField.c_str(), strQuote.c_str(), strValue.c_str(), strQuote.c_str());
  156.     break;
  157.      case soNoEndsWith:
  158.     wsprintf(szTemp, "%s NOT LIKE %s%%%s%s", strField.c_str(), strQuote.c_str(), strValue.c_str(), strQuote.c_str());
  159.     break;
  160.      case soContains:
  161.     wsprintf(szTemp, "%s LIKE %s%%%s%%%s", strField.c_str(), strQuote.c_str(), strValue.c_str(), strQuote.c_str());
  162.     break;
  163.      case soNoContains:
  164.     wsprintf(szTemp, "%s NOT LIKE %s%%%s%%%s", strField.c_str(), strQuote.c_str(), strValue.c_str(), strQuote.c_str());
  165.     break;
  166.      case soBlank:
  167.     wsprintf(szTemp, "%s IS NULL", strField.c_str());
  168.     break;
  169.      case soNotBlank:
  170.     wsprintf(szTemp, "%s IS NOT NULL", strField.c_str());
  171.     break;
  172.      default:
  173.     szTemp[0] = '\0';
  174.   }
  175.   strWhere = szTemp;
  176.  
  177.     szTemp[0] = '\0';
  178.   if (ComboBox1->ItemIndex == soNoCondition)
  179.     wsprintf(szTemp, "SELECT * FROM \"%s\"", strTable.c_str());
  180.   else if (Table1->FieldByName(strField)->DataType == ftString)
  181.     wsprintf(szTemp, "SELECT * FROM \"%s\" t WHERE t.%s", strTable.c_str(), strWhere.c_str());
  182.   else
  183.     wsprintf(szTemp, "SELECT * FROM \"%s\" t WHERE t.%s", strTable.c_str(), strWhere.c_str());
  184.   strQuery = szTemp;
  185.  
  186.   // Create an instance of the browser form.
  187.   //frmQuery.reset(new TResultForm(Application));
  188.   // New auto_ptr<> behavior
  189.   std::auto_ptr<TResultForm> tmp (new TResultForm(Application));
  190.   frmQuery = tmp;
  191.   tmp.release();
  192.  
  193.   szTemp[0] = '\0';
  194.  
  195.   Screen->Cursor = Controls::TCursor(crHourGlass);
  196.   if (frmQuery->Query1->Active)
  197.     frmQuery->Query1->Close();
  198.   frmQuery->Query1->DatabaseName = strAlias; // set the alias the query poitns to
  199.   frmQuery->Query1->SQL->Clear();            // empty existing SQL in the query
  200.   frmQuery->Query1->SQL->Add(strQuery);      // add query string to query object
  201.   frmQuery->Query1->Active = True;           // try to run the query
  202.   Screen->Cursor = Controls::TCursor(crDefault);
  203.  
  204.   if (frmQuery->Query1->Active){
  205.     /* If the query didn't return any records, there's no point in
  206.        displaying the form.  In that event, raise an exception. */
  207.     if (frmQuery->Query1->RecordCount < 1)
  208.       throw Exception("No records matched your criteria.  " \
  209.               "Please try again.");
  210.  
  211.     // write a message to the browse form's status line
  212.     if (strField.Length() == 0){
  213.        wsprintf(szTemp, "Now showing all records from %s ...", strTable.c_str());
  214.        frmQuery->Panel3->Caption = szTemp;
  215.     }
  216.     else {
  217.        wsprintf(szTemp, "Now showing %s where %s contains values equal to ..."
  218.       , strTable.c_str(), strField.c_str(), strValue.c_str());
  219.        frmQuery->Panel3->Caption = szTemp;
  220.     }
  221.     // show the form
  222.     frmQuery->ShowModal();
  223.   }
  224. }
  225. //---------------------------------------------------------------------
  226. void __fastcall TQueryForm::ListBox1Click(TObject *Sender)
  227. {
  228.   AnsiString strValue;       // Holds the alias selected by the user
  229.  
  230.   // Determine the alias name selected by the user }
  231.   strValue = ListBox1->Items->Strings[ListBox1->ItemIndex];
  232.  
  233.   /*
  234.     Get the names of the tables in the alias and put them in the
  235.     appropriate list box, making sure the user's choices are reflected
  236.     in the list.
  237.   */
  238.   ListBox2->Items->Clear();
  239.   Session->GetTableNames(strValue,          // alias to enumerate
  240.             "",                 // pattern to match
  241.             CheckBox1->Checked, // show extensions flag
  242.             CheckBox2->Checked, // show system tables flag
  243.             ListBox2->Items);   // target for table list
  244.  
  245.   /*
  246.     Make sure there are tables defined in the alias.  If not, show an
  247.     error; otherwise, clear the list box.
  248.   */
  249.   Screen->Cursor = Controls::TCursor(crDefault);
  250.   if (ListBox2->Items->Count < 1)
  251.     MessageDlg("There are no tables in the alias you selected.  Please " \
  252.            "choose another", mtError, TMsgDlgButtons() << mbOK, 0 );
  253.  
  254.   ListBox3->Items->Clear();
  255. }
  256. //---------------------------------------------------------------------
  257. void __fastcall TQueryForm::ListBox2Click(TObject *Sender)
  258. {
  259.   Screen->Cursor = Controls::TCursor(crHourGlass);
  260.   try
  261.   {
  262.     // First, disable the TTable object.
  263.     if (Table1->Active) Table1->Close();
  264.  
  265.     // Open the selected table
  266.     Table1->DatabaseName = ListBox1->Items->Strings[ListBox1->ItemIndex];
  267.     Table1->TableName = ListBox2->Items->Strings[ListBox2->ItemIndex];
  268.  
  269.     // Open the table and put a list of the field names in the Fields list box. }
  270.     Table1->Open();
  271.     if (Table1->Active) Table1->GetFieldNames(ListBox3->Items);
  272.   }
  273.   catch (...) {}
  274.   Screen->Cursor = Controls::TCursor(crDefault);
  275. }
  276. //---------------------------------------------------------------------
  277.